<template>
  <div class="login">
    <!-- <div class="title-logo">
      <img  :src="logoUrl" alt="">
    </div> -->
    <div class="lang-box">
      <svg-icon style="font-size: 18px;color: #fff;cursor: pointer;vertical-align: middle;" icon-class="language"   />
      <el-select v-model="lang" class="lang-select" placeholder="请选择" @change="handleSelectLanguage">
        <el-option
          v-for="(value, key, index) in langList"
          :key="key"
          :label="value.language"
          :value="key">
        </el-option>
      </el-select>
    </div>
    <div class="login-main">
      <div class="info-wrap">
        <div class="info-header">
          <div class="info-logo-box">
            <img src="../assets/logo/xz-logo-w.png" alt="">
          </div>
          <div class="info-title" >{{$t('login.title')}}</div>
        </div>
        <div class="info-details">
          <p>{{$t('login.desc1')}}</p>
          <p style="margin-top: 10px;">{{$t('login.desc2')}}</p>
        </div>
      </div>
      <div class="login-wrap">
        <div class="form-logo">
          <img v-if="logoUrl" src="../assets/logo/xzlogo.png" alt="">
          <img v-else src="../assets/logo/logo-en.png" alt="">
        </div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">{{$t('login.login')}}</h3>
          <el-form-item prop="username" style="margin-bottom: 30px;">
            <el-input
              class="input"
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              :placeholder="$t('login.username')"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password" style="margin-bottom: 30px;">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              :placeholder="$t('login.password')"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaOnOff">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              :placeholder="$t('login.verification')"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;color:#fff;">{{$t('login.remember')}}</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
              class="login-btn"
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">{{$t('login.login')}}</span>
              <span v-else>{{$t('login.logining')}}</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span class="copyright">粤ICP备18134240号-1</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg,getCinemaAccessToken } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import Logo from "../layout/components/Sidebar/Logo.vue";
import { i18n } from '../i18n/index';
import { setLocale,getLocale } from '../utils/locale';

export default {
  name: "Login",
  components: {Logo },
  data() {
    return {
      lang:i18n.locale,
      codeUrl: "",
      backgrounds:[
        require('../assets/images/bg/1.jpg'),
        require('../assets/images/bg/2.jpg'),
        require('../assets/images/bg/3.jpg'),
        require('../assets/images/bg/4.jpg'),
        require('../assets/images/bg/5.jpg'),
      ],
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: this.$t('login.usernameEmpty') }
        ],
        password: [
          { required: true, trigger: "blur", message: this.$t('login.passwordEmpty') }
        ],
        code: [{ required: true, trigger: "change", message: this.$t('login.verificationEmpty') }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
      locale:getLocale()
    };
  },
  computed:{
    logoUrl(){
      return i18n.locale=='zh';
    },
    langList(){
      return i18n.messages;
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            // this.$router.push({ path: this.redirect || "/cloudaiot-web" }).catch(()=>{});
            this.$router.push({ path: "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
    handleSelectLanguage(value){
      //i18n.locale = value;
      setLocale(value);
      location.reload();
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  background-image: url('../assets/images/bg/bg.jpg');
  background-size: 100% 100%;
  overflow: hidden;
  ::-webkit-scrollbar{
    width: 0;
    stroke-width: 0;
  }
  .login-main{
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: center;
    margin-top: 90px;
    box-sizing: content-box;
  }
  .info-wrap{
    margin-right: 100px;
    .info-header{
      .info-logo-box{
        width: 138px;
        height: 138px;
        display: inline-block;
        vertical-align: middle;
        img{width: 100%;height: 100%;}
      
      }
      .info-title{
        width: 600px;
        font-size: 60px;
        color: #fff;
        font-weight: bold;
        display: inline-block;
        vertical-align: middle;
        margin-left: 35px;
        // word-break: break-all;
      }
    }
    .info-details{
      width: 800px;
      margin-top: 30px;
      font-size: 21px;
      color: #fff;
      line-height: 40px;
      letter-spacing: 0;
    }
  }
  .login-wrap{
    border-radius: 6px;
    background: rgba(17, 127, 142, 0.2);
    width: 610px;
    height: 756px;
    padding: 105px 82px 5px 82px;
    box-sizing: border-box;
    .form-logo{
      height: 105px;
      img{
        height: 100%;
      }
    }
    .title {
      margin-top: 50px ;
      margin-bottom: 50px ;
      text-align: left;
      font-size: 36px;
      color: #91e1ec;
    }
  }
  .el-login-footer{
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
    z-index: 999;
  }
  .lang-box{
    position: fixed;
    top: 10px;
    right: 10px;
    background-color: none;
    .lang-select{
      display: inline-block;
      vertical-align: middle;
      height: 30px;
      margin-top: -5px;
    }
  }
}


.login-form {
  box-sizing: border-box;
  .el-input {
    width: 446px;
    height: 66px;
    border-radius: 0;
  }
  .input-icon {
    height: 56px;
    width: 22px;
    margin-left: 2px;
  }
  .login-btn{
    width: 430px;
    height: 66px;
    line-height: 66px;
    padding: 0;
    border-radius: 0px;
    font-size: 18px;
    background-image:linear-gradient(to right,#579bac,#09718d,#579bac);
    border: none;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
.login .title-logo{
    height: auto;
    width: 80%;
    position: absolute;
    top: 60px;
    left: 10%;
    img{
      max-height: 80px;
    }
}
.copyright{
  color: #fff;
  font-size: 16px;
}
::v-deep .lang-select .el-input__inner{
  display: inline-block;
  width: 100px;
  height: 36px;
  line-height: 36px;
  background: none !important;
  font-size: 13px;
  color: #fff;
  padding-left: 20px;
}
::v-deep .el-input__inner{
  width: 100%;
  height: 66px;
  line-height: 66px;
  // border-radius: 30px;
  padding-left: 70px;
  font-size: 18px;
  background-image:linear-gradient(to right,rgba(13, 72, 101, 1),rgba(14,94,121,1),rgba(11,58,81,1));
  border: none;
  color:#fff;
}
::v-deep .el-input__prefix{
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  flex: 0 0 auto;
}
</style>